#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100005;
typedef long long ll;
const ll inf = 1LL << 60;
ll p[MAXN];

struct brick{
	long long m, d;
}b[MAXN];

bool cmp(brick x, brick y){
	return x.d < y.d;
}

int main(){
	int t, N, T;
	scanf("%d", &t);
	while(t--){
		scanf("%d", &N);
		for(int i=0;i<N;i++){
			scanf("%lld %lld", &b[i].m, &b[i].d);
		}
		scanf("%d", &T);
		p[0] = 0;
		for(int i=1;i<=T;i++){
			scanf("%lld", &p[i]);
		}
		p[T+1] = inf;
		
		sort(b, b+N, cmp);
		sort(p+1, p+1+T);
		
		long long score = 0, sum = 0;
		for(int i=0, j=1; i<N;){
			if(sum + b[i].m < p[j]){
				score += 1LL * j * b[i].m * b[i].d;
				sum += b[i].m;
				i++;
			}else{
				ll tem = p[j] - sum;
				b[i].m -= tem;
				score += 1LL * j * tem * b[i].d;
				sum += tem;
				j++; 
			}
		}
		printf("%lld\n", score);
	}
	return 0;
}
